/*
 moxl - Modular XML Library
 Copyright (c) 2007, Malcolm Sparks

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.xpath.parser.types;

import java.util.Iterator;
import java.util.List;

import moxl.dom.MoxlAttribute;
import moxl.dom.MoxlCharacterData;
import moxl.dom.MoxlNode;

public class XPathNodeSetValue extends AbstractXPathValue
{
	private final List<MoxlNode> nodeSet;

	public XPathNodeSetValue(List<MoxlNode> nodeSet)
	{
		this.nodeSet = nodeSet;
	}

	@Override
	protected String getTypeName()
	{
		return "node set";
	}

	public boolean containsNodeWithSpecificStringValue(String valueAsString)
	{
		for (Iterator<MoxlNode> it = nodeSet.iterator(); it.hasNext();)
		{
			MoxlNode node = it.next();

			// TODO: Differentiate between each node type- what does the XPath spec. say?

			// if (valueAsString.equals(node.getData()))
			// {
			// return true;
			// }
			if (node instanceof MoxlAttribute)
			{
				return valueAsString.equals(((MoxlAttribute) node).getValue());
			}
			else if (node instanceof MoxlCharacterData)
			{
				return valueAsString.equals(((MoxlCharacterData) node).getData());
			}
			else
			{
				throw new UnsupportedOperationException("TODO: node is " + node.getClass().getName());
			}
		}
		return false;
	}

	public MoxlNode getLastNode()
	{
		if (nodeSet.isEmpty())
		{
			throw new IllegalStateException("No last node, node list is empty");
		}
		return nodeSet.get(nodeSet.size() - 1);
	}
}
